package com.shuangti.food.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(@Autowired LettuceConnectionFactory connectionFactory){ // 连接工厂类
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); // 定义模板类

        redisTemplate.setConnectionFactory(connectionFactory); // 配置连接工厂

        // 追加RedisTemplate序列化管理操作
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key采用的是字符串方式管理
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // JDk序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        return redisTemplate;
    }
}
